﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EbayMaster
{
    public partial class FrmEbayFees : Form
    {
        public FrmEbayFees()
        {
            InitializeComponent();
        }

        public enum ItemCategoryType
        {
            Electronics = 0,
            ClothingShoesAndAccessories,
            BooksMoviesMusic,
            AllOtherCategories
        }

        private double getDoubleValFromTextBox(TextBox tb)
        {
            double retVal = -1.0;

            if (tb.Text == null || tb.Text.Trim() == "")
                return retVal;

            if (!Double.TryParse(tb.Text, out retVal))
            {
                MessageBox.Show("输入有误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return retVal;
            }

            return retVal;
        }

        private void buttonCalProfit_Click(object sender, EventArgs e)
        {
            double ebaySalePrice = getDoubleValFromTextBox(this.textBoxEbaySalePrice);
            if (-1.0 == ebaySalePrice)
                return;

            double itemCost = getDoubleValFromTextBox(this.textBoxItemCost);
            if (-1.0 == itemCost)
                return;

            double shippingCost = getDoubleValFromTextBox(this.textBoxShippingFee);
            if (-1.0 == shippingCost)
                return;

            double insertionFee = getDoubleValFromTextBox(this.textBoxEbayInsertionFee);
            if (-1.0 == insertionFee)
                return;

            string itemCategoryStr = this.comboBoxItemCategory.Text;
            if (itemCategoryStr == null || itemCategoryStr.Trim() == "")
            {
                MessageBox.Show("请选择一个商品类别", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ItemCategoryType itemCategory = ItemCategoryType.AllOtherCategories;
            {
                if (0 == string.Compare(itemCategoryStr, "Electronics"))
                    itemCategory = ItemCategoryType.Electronics;
                else if (0 == string.Compare(itemCategoryStr, "ClothingShoesAndAccessories"))
                    itemCategory = ItemCategoryType.ClothingShoesAndAccessories;
                else if (0 == string.Compare(itemCategoryStr, "BooksMoviesMusic"))
                    itemCategory = ItemCategoryType.BooksMoviesMusic;
            }

            double fvfFee = 0.0;
            double paypalFee = 0.0;
            double profit = 0.0;

            profit = System.Math.Round(ebaySalePrice * EbayConstants.DollarToRMB, 2);
            profit -= itemCost;
            profit -= shippingCost;
            profit -= System.Math.Round(insertionFee * EbayConstants.DollarToRMB, 2);
            // fvf
            {
                switch (itemCategory)
                {
                    case ItemCategoryType.AllOtherCategories:
                        fvfFee = ebaySalePrice * 0.11;
                        break;
                    case ItemCategoryType.Electronics:
                        fvfFee = ebaySalePrice * 0.07;
                        break;
                    case ItemCategoryType.BooksMoviesMusic:
                        fvfFee = ebaySalePrice * 0.13;
                        break;
                    case ItemCategoryType.ClothingShoesAndAccessories:
                        fvfFee = ebaySalePrice * 0.10;
                        break;
                }
                fvfFee = System.Math.Round(fvfFee, 2);
                this.textBoxEbayFVF.Text = fvfFee.ToString();
            }
            profit -= System.Math.Round(fvfFee * EbayConstants.DollarToRMB, 2);
            //paypalFee
            {
                paypalFee = System.Math.Round(ebaySalePrice * 0.039 + 0.3, 2);
                this.textBoxPayPalFee.Text = paypalFee.ToString();
            }
            profit -= System.Math.Round(paypalFee * EbayConstants.DollarToRMB, 2);

            this.textBoxProfit.Text = profit.ToString();
        }

        public class ItemCompactInfo
        {
            public int ItemId;
            public string ItemName;

            public ItemCompactInfo(int itemId,string itemName)
            {
                this.ItemId = itemId;
                this.ItemName = itemName;
            }

            public override string ToString()
            {
                return string.Format("{0} ", ItemName);
            }
        }   // class ItemCompactInfo

        private void LoadAllItems()
        {
            DataTable dtAllItems = ItemDAL.GetAllItems();
            foreach (DataRow dr in dtAllItems.Rows)
            {
                int itemId = (int)dr["ItemId"];
                string itemName = dr["ItemName"].ToString();

                ItemCompactInfo itemInfo = new ItemCompactInfo(itemId, itemName);
                this.listBoxItem.Items.Add(itemInfo);
            }
        }

        private void FrmEbayFees_Load(object sender, EventArgs e)
        {
            this.comboBoxItemCategory.Text = "AllOtherCategories";

            LoadAllItems();
        }

        private void listBoxItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!this.checkBoxUseItemInfo.Checked)
                return;

            ItemCompactInfo itemInfo = (ItemCompactInfo)this.listBoxItem.SelectedItem;
            if (null == itemInfo)
                return;

            InventoryItemType itemType = ItemDAL.GetItemById(itemInfo.ItemId);
            if (null == itemType)
                return;

            this.textBoxItemCost.Text = itemType.ItemCost.ToString();
            this.textBoxShippingFee.Text = itemType.ItemShippingCost.ToString();
        }
    }
}
